import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: '',
    userInfo: JSON.parse(sessionStorage.getItem("userInfo"))//反序列化，json转为string
  },
  mutations: {
  //类似于set方法
    SET_TOKEN: (state, token) =>{
      state.token = token;
      localStorage.setItem("token",token)//存入全局里面
    },

    SET_USERINFO: (state, userInfo) =>{
      state.userInfo = userInfo;
      sessionStorage.setItem("userInfo",JSON.stringify(userInfo))
    },

    REMOVE_INFO: (state) =>{
      state.token = '';
      state.userInfo = {};
      localStorage.setItem("token","");
      sessionStorage.setItem("userInfo",JSON.stringify(""));
    }
  },
  getters:{
    //get方法
    getUser: state =>{
      return state.userInfo;
    }
  },
  actions: {

  },
  modules: {

  }

  }
)
